package com.qingyun.edusysteam.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")          // 拦截所有请求
                .excludePathPatterns(            // 排除以下路径
                        "/user/login",           // 登录接口
                        "/error",                // 错误页面
                        "/favicon.ico",          // 网站图标
                        "/static/**",            // 静态资源
                        "/css/**",               // CSS文件
                        "/js/**",                // JavaScript文件
                        "/images/**",            // 图片文件
                        "/login.html",           // 登录页面
                        "/index.html",           // 首页（需要验证）
                        "/teacher/**"            // 教师端页面（需要验证）
                );
    }
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }
}